package com.his.controller;

import com.his.annotation.RequireRoles;
import com.his.common.Const;
import com.his.common.PageParam;
import com.his.common.Rsp;
import com.his.common.Temp;
import com.his.entity.ApplicantInfo;
import com.his.entity.Application;
import com.his.service.ApplicationService;
import com.his.utils.MyUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (Application)表控制层
 *
 * @author makejava
 * @since 2020-10-23 18:04:38
 */
@RestController
@RequestMapping("application")
@Validated
public class ApplicationController {
    /**
     * 服务对象
     */
    @Resource
    private ApplicationService applicationService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Application selectOne(Integer id) {
        return this.applicationService.queryById(id);
    }

    /**
     * 显示所有数据,带分页，带状态查询
     *
     * @return
     */
    @PostMapping("selectAll")
    @RequireRoles(roles = {"nurse", "doctor","medicinal","admin","hr"}, index = 0)
    public Rsp selectAll(@RequestHeader(Const.TOKEN) String token, @RequestBody PageParam<Application> pageParam) throws Exception {
        return this.applicationService.queryAllApply(pageParam,MyUtil.getUser(token));
    }

    /**
     * 医生，或病人申请表插入申请表
     *
     * @param application
     * @return
     */
    @PostMapping("insertOne")
    @RequireRoles(roles = {"patient"}, index = 0)
    public Rsp insertOne(@RequestHeader(Const.TOKEN) String token
            , @Validated @RequestBody Application application) throws Exception {
        return this.applicationService.insertOne(token, application);
    }

    /**
     * 护士，或医生更新操作
     *
     * @param token
     * @param application
     * @return
     */
    @PostMapping("updateOne")
    @RequireRoles(roles = {"doctor", "nurse","hr"}, index = 0)
    public Rsp updateOne(@RequestHeader(Const.TOKEN) String token, @RequestBody Application application) {
        return this.applicationService.updateOne(application);
    }

    /**
     * 查询通过病人id
     *
     * @param token
     * @param patientId
     * @return
     */
    @GetMapping("selectOneByPatientId")
    @RequireRoles(roles = {"doctor", "nurse"}, index = 0)
    public Rsp selectOneByPatientId(@RequestHeader(Const.TOKEN) String token, String patientId) {
        return Rsp.success(applicationService.selectOneByPatientId(patientId));
    }

    @GetMapping("getByPatientId")
    @RequireRoles(roles = {"patient"}, index = 0)
    public Rsp getByPatientId(@RequestHeader(Const.TOKEN) String token) throws Exception {
        return Rsp.success(applicationService.getByPatientId(MyUtil.getUserByToken(token)));
    }

    @PostMapping("insertByTemp")
    @RequireRoles(roles = {"nurse", "doctor"}, index = 0)
    public Rsp insertByTemp(@RequestHeader(Const.TOKEN) String token, @Validated @RequestBody Temp temp) throws Exception {
        return applicationService.insertByTemp(temp);
    }

    @GetMapping("getByNo")
    public Rsp getByNo(@RequestHeader(Const.TOKEN) String token, String no) {
        return applicationService.getByNo(no);
    }

    /**
     * 病历表病人表申请表查询返回vo类
     *
     * @param token
     * @param applicantInfoPageParam
     * @return
     */
    @RequireRoles(roles = {"nurse","admin"}, index = 0)
    @PostMapping("getApplicantInfo")
    public Rsp getAllApplicantInfo(@RequestHeader(Const.TOKEN) String token, @RequestBody PageParam<ApplicantInfo> applicantInfoPageParam) throws Exception {
        return Rsp.success(applicationService.getAllApplicantInfo(applicantInfoPageParam,MyUtil.getUser(token)));
    }

    @RequireRoles(roles = {"nurse","admin"}, index = 0)
    @PostMapping("getAllApplicantInfoByAdvice")
    public Rsp getAllApplicantInfoByAdvice(@RequestHeader(Const.TOKEN) String token, @RequestBody PageParam<ApplicantInfo> applicantInfoPageParam) throws Exception {
        return Rsp.success(applicationService.getAllApplicantInfoByAdvice(applicantInfoPageParam,MyUtil.getUser(token)));
    }

}
